package api

import (
	"api-gateway/basic/config"
	__ "api-gateway/basic/proto"
	"api-gateway/handler/request"
	"github.com/gin-gonic/gin"
	"net/http"
)

// 添加收藏
func AddCollect(c *gin.Context) {
	var req request.AddCollectReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数有误",
			"data": err.Error(),
		})
		return
	}

	addCollect, err := config.CourseClient.AddCollect(c, &__.AddCollectReq{
		UserId:   req.UserId,
		CourseId: req.CourseId,
	})

	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 500,
			"msg":  "服务端错误",
			"data": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "收藏成功",
		"data": addCollect.CollectId,
	})
}

// 收藏列表
func GetCollectList(c *gin.Context) {
	var req request.GetCollectListReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数有误",
			"data": err.Error(),
		})
		return
	}

	collectList, err := config.CourseClient.GetCollectList(c, &__.GetCollectListReq{
		UserId: req.UserId,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 500,
			"msg":  "服务端错误",
			"data": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "我的收藏列表展示成功",
		"data": collectList,
	})
}
